package main

import "fmt"

//用go语言实现5行5列盘龙数组

// 创建盘龙数组
func create(rows, cols int) [][]int {
	//初始化盘龙数组
	arr := make([][]int, rows) //长度为rows
	for i := range arr {
		arr[i] = make([]int, cols) //宽度为cols
	}
	num := 1           //记录盘龙数组的每个元素值
	startrow := 0      //起始行
	endrow := rows - 1 //结束行
	startcol := 0      //起始列
	endcol := cols - 1 //结束列
	for num <= rows*cols {
		//从左往右
		for i := startcol; i <= endcol; i++ {
			arr[startrow][i] = num
			num++
		}
		startrow++
		//从上往下
		for i := startrow; i <= endrow; i++ {
			arr[i][endcol] = num
			num++
		}
		endcol--
		//从右往左
		for i := endcol; i >= startcol; i-- {
			arr[endrow][i] = num
			num++
		}
		endrow--
		//从下往上
		for i := endrow; i >= startrow; i-- {
			arr[i][startcol] = num
			num++
		}
		startcol++
	}
	return arr
}

func main() {
	rows := 5
	cols := 5
	arr := make([][]int, rows, cols)
	copy(arr, create(rows, cols))
	for _, r := range arr {
		for _, c := range r {
			fmt.Printf("%3d", c)
		}
		fmt.Println()
	}
}
